﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WenSkin.Controls;
using static WenSkin.Forms.LeftButtonForm;

namespace WenFrame
{
    public class WenUserControlPower
    {
        public WenUserControlPower(string name)
        {
            SelectPower(UserInfo.UserName, name);
        }
        public WenUserControlPower(WenUserControl owner)
        {
            this.owner = owner;
            SelectPower(UserInfo.UserName, owner.Name);
        }

        public WenUserControlPower(string name, WenToolStrip owner)
        {
            wenToolStrip = owner;
            SelectPower(UserInfo.UserName, name);
        }

        #region 私有属性
        private readonly WenUserControl owner;
        private readonly WenToolStrip wenToolStrip;
        public List<VisPower> VisPowers = new List<VisPower>();
        private WenSkin.SQL.SqlConnect SqlConnect = new WenSkin.SQL.SqlConnect();
        #endregion

        //从服务器获取可用权限
        private void SelectPower(string username, string name)
        {
            string sql = $"select [PowerName],[PowerState] from [w_user_power] where ( username=N'{username}' or username=N'SystemAllRoot' ) and UserControlName=N'{name}'";
            DataTable dt = SqlConnect.SqlDataTable(sql);
            if (dt.Rows.Count == 0)
                return;
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    string n = dr["PowerName"]?.ToString();
                    bool p = (int)dr["PowerState"] == 1;

                    VisPowers.Add(new VisPower(n, p));
                }
                catch
                {
                }
            }
        }

        //设置权限
        public void SetPower()
        {
            if (owner != null)
            {
                foreach (var item in owner.Controls)
                {
                    if (item is WenToolStrip toolStrip)
                    {
                        SetPower(toolStrip);
                    }
                }
            }
            if (wenToolStrip != null)
            {
                SetPower(wenToolStrip);
            }
        }

        private void SetPower(WenToolStrip toolStrip)
        {
            foreach (var tool in toolStrip.Items)
            {
                switch (tool)
                {
                    case ToolStripButton button:
                        {
                            if ((VisPowers.Find(q => q.Text.Contains(button.Text))?.State) is bool)
                                button.Visible = true;
                            else
                                button.Visible = false;
                            break;
                        }

                    case ToolStripCheckBox checkBox:
                        {
                            if ((VisPowers.Find(q => q.Text.Contains(checkBox.Text))?.State) is bool)
                                checkBox.Visible = true;
                            else
                                checkBox.Visible = false;
                            break;
                        }
                    case ToolStripWenComboBox comboBox:
                        {
                            if ((VisPowers.Find(q => q.Text.Contains(comboBox.PowerText))?.State) is bool)
                                comboBox.Visible = true;
                            else
                                comboBox.Visible = false;
                            break;
                        }
                    case ToolStripWenTextBox textBox:
                        {
                            if ((VisPowers.Find(q => q.Text.Contains(textBox.PowerText))?.State) is bool)
                                textBox.Visible = true;
                            else
                                textBox.Visible = false;
                            break;
                        }
                }
            }
        }

        public void SetPower(LeftMenuItemCollection leftMenu)
        {
            foreach (LeftMenuItem item in leftMenu)
            {
                if ((VisPowers.Find(q => q.Text.Contains(item.Text))?.State) is bool)
                    item.Visible = true;
                else
                    item.Visible = false;
            }
        }
        public class VisPower
        {
            public VisPower(string text, bool state)
            {
                Text = text;
                State = state;
            }

            public string Text { get; set; }
            public bool State { get; set; }
        }
    }
}
